package com.ht.aems.fesmonitor.actions.comm;

import com.ht.aems.fesmodel.CommUnit;
import com.ht.aems.fesmonitor.AllBufferDataFrame;
import com.ht.aems.fesmonitor.BufferDataFrame;
import com.ht.aems.fesmonitor.FesMonitor;
import com.ht.aems.fesmonitor.HisBufferDataQueryFrame;
import com.ht.aems.swing.util.MyAction;
import com.ht.aems.swing.util.SwingUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2008-7-19
 */
public class ShowAllBufData extends MyAction {
    List<BufferDataFrame> dataFrames;
    AllBufferDataFrame allBufferDataFrame;
    public void actionPerformed(ActionEvent e) {
        FesMonitor monitor = (FesMonitor) this.getAncestor();
        List<CommUnit> units = monitor.getConnectCommUnits();
        dataFrames = new ArrayList<BufferDataFrame>(units.size());
        for (CommUnit unit : units) {
            if (unit == null)
                return;
            boolean isOpend = false;
            Window[] ownerlessWindows = JFrame.getOwnerlessWindows();
            if (ownerlessWindows != null && ownerlessWindows.length > 1) {
                for (Window window : ownerlessWindows) {
                    if (window instanceof BufferDataFrame) {
                        BufferDataFrame bufferDataFrame = (BufferDataFrame) window;
                        if (bufferDataFrame.getDeviceName().equals(unit.getName()) && bufferDataFrame.isVisible()) {
                            isOpend = true;
//                            bufferDataFrame.setVisible(true);
                            dataFrames.add(bufferDataFrame);
                            break;
                        }
                    }
                }

            }
            if (!isOpend) {
               if (SwingUtil.showAlreadyExsistFrame(BufferDataFrame.class))
                 return;
                BufferDataFrame bufferDataFrame = new BufferDataFrame(unit.getId(), unit.getName());
                //bufferDataFrame.setVisible(true);
                dataFrames.add(bufferDataFrame);
            }
        }
        allBufferDataFrame = new AllBufferDataFrame(dataFrames);
        allBufferDataFrame.setVisible(true);
    }

    public void update(AbstractButton source) {
        FesMonitor monitor = (FesMonitor) this.getAncestor();
        if (monitor.getConnectCommUnits().size() < 1) {
            source.setEnabled(false);
            return;
        }

        source.setEnabled(true);
    }
}